const {con} = require('../db/db');

async function getLogisticsList(page) {
    const sql = `select * from logistics order by top desc limit ${(page-1)*10},10;`;

    const total = `select * from logistics;`;

    let [rows] = await con.execute(sql);

    let [totals] = await con.execute(total);

    return {data:rows,total:totals.length}
}

async function addLogistics(name,pathway,address,telephone) {
    const sql = `insert into logistics(name,pathway,address,telephone) values("${name}","${pathway}","${address}","${telephone}");`;

    await con.execute(sql);
}

async function editLogistics(name,pathway,address,telephone,id) {
    const sql = `update logistics set name="${name}",pathway="${pathway}",address="${address}",telephone="${telephone}" where id=${id};`;

    await con.execute(sql);
}

async function delLogisticsById(id) {
    const sql = `delete from logistics where id=${id};`;

    await con.execute(sql);
}

async function changeTop(id,top) {
    const sql = `update logistics set top=${top} where id=${id};`;

    await con.execute(sql);
}

async function searchWord(search) {
    const sql = `select * from logistics where name like '%${search}%';`;

    let [rows] = await con.execute(sql);

    return rows
}

module.exports={
    getLogisticsList,
    addLogistics,
    editLogistics,
    delLogisticsById,
    changeTop,
    searchWord
}